package com.cql.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.core.DO.SysMenuDO;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.SysMenuMapper;
import com.cql.service.business.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author cql
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuDO> implements SysMenuService {


    @Autowired
    private SysMenuMapper sysMenuMapper;


    @Override
    public Set<String> listMenuPermsByUserId(Long userId) {
        /**
         * 查询用户菜单权限
         */
        List<SysMenuDO> perms = sysMenuMapper.listMenuPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (SysMenuDO perm : perms) {
            if (StringUtils.isNotNull(perm)) {
                /**
                 * 有可能一个菜单对应的权限字符串有多个，且用逗号隔开
                 */
                permsSet.addAll(Arrays.asList(perm.getPerms().trim().split(",")));

            }
        }

        return permsSet;
    }
}